
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Template," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>handlebars显示对象的键 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        handlebars显示对象的键
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Template/" rel="tag">Ember-Template</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 3 分钟</span>
    </header>

    <div class="post-content">
      <p>在实际的开发过程中你很有可能需要显示出对象数组的键或者值，如果你需要同时显示出对象的键和值你可以使用<code>{{#each-in}}</code>标签。<br><br>注意：<code>each-in</code>标签是<code>Ember 2.0</code>才有的功能，之前的版本是无法使用这个标签的，如果是2.0一下的版本会报错：<code>Uncaught Error: Assertion Failed: A helper named &#39;each-in&#39; coulad not be found</code><br><br>准备工作：使用<code>Ember CLI</code>生成一个<code>component</code>，与此同时会生成一个对应的模板文件。<br><br><code>ember generate component store-categories</code><br><br>执行上述命令得到下面的3个文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">app&#x2F;components&#x2F;store-categories.js</span><br><span class="line">app&#x2F;templates&#x2F;components&#x2F;store-categories.hbs</span><br><span class="line">tests&#x2F;integration&#x2F;components&#x2F;store-categories-test.js</span><br></pre></td></tr></table></figure>
<p>然后在<code>app/router.js</code>增加一个路由设置，在<code>map</code>方法里添加<code>this.route(&#39;store-categories&#39;);</code>；此时可以直接访问<a target="_blank" rel="noopener" href="http://localhost:4200/store-categories">http://localhost:4200/store-categories</a>;</p>
<p><a target="_blank" rel="noopener" href="http://guides.emberjs.com/v2.0.0/templates/displaying-the-keys-in-an-object/">http://guides.emberjs.com/v2.0.0/templates/displaying-the-keys-in-an-object/</a></p>
<h4 id="在组件中增加测试数据"><a href="#在组件中增加测试数据" class="headerlink" title="在组件中增加测试数据"></a>在组件中增加测试数据</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// app/components/store-categories.js</span></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    <span class="comment">// https://guides.emberjs.com/v2.4.0/components/the-component-lifecycle/</span></span><br><span class="line">    willRender: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">//  设置一个对象到属性“categories”上，并且设置到categories属性上的对象结构是：key为字符串，value为数组</span></span><br><span class="line">        <span class="built_in">this</span>.set(<span class="string">&#x27;categories&#x27;</span>, &#123;</span><br><span class="line">          <span class="string">&#x27;Bourbons&#x27;</span>: [<span class="string">&#x27;Bulleit&#x27;</span>, <span class="string">&#x27;Four Roses&#x27;</span>, <span class="string">&#x27;Woodford Reserve&#x27;</span>],</span><br><span class="line">          <span class="string">&#x27;Ryes&#x27;</span>: [<span class="string">&#x27;WhistlePig&#x27;</span>, <span class="string">&#x27;High West&#x27;</span>]</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">));</span><br></pre></td></tr></table></figure>

<p><code>willRender</code>方法在组件渲染的时候执行，更多有关组件的介绍会在后面章节——组件中介绍，想了解更多有关组件的介绍会在后面的文章中一一介绍，目前你暂且把组件当做是一个提取出来的公共HTML代码。</p>
<p>有了测试数据之后我们怎么去使用<code>each-in</code>标签遍历出数组的键呢？</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- // app/templates/components/store-categories.hbs --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">  &#123;&#123;#each-in categories as |category products|&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>&#123;&#123;category&#125;&#125;</span><br><span class="line">      <span class="tag">&lt;<span class="name">ol</span>&gt;</span></span><br><span class="line">        &#123;&#123;#each products as |product|&#125;&#125;</span><br><span class="line">          <span class="tag">&lt;<span class="name">li</span>&gt;</span>&#123;&#123;product&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">        &#123;&#123;/each&#125;&#125;</span><br><span class="line">      <span class="tag">&lt;/<span class="name">ol</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">  &#123;&#123;/each-in&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>上述模板代码中第一个位置参数<code>category</code>就是迭代器的键，第二个位置参数<code>product</code>就是键所对应的值。</p>
<p>为了显示效果，在<code>application.hbs</code>中调用这个组件，组件的调用非常简单，直接使用<code>{{组件名}}</code>方式调用。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- //app/templates/application.hbs --&gt;</span></span><br><span class="line">&#123;&#123;store-categories&#125;&#125;</span><br></pre></td></tr></table></figure>

<p>渲染后结果如下图：</p>
<p><img src="/content/images/2016/03/23.png" alt="result"></p>
<h3 id="重渲染"><a href="#重渲染" class="headerlink" title="重渲染"></a>重渲染</h3><p><strong><code>{{each-in}}</code>表达式不会根据属性值变化而自动更新。</strong>上述示例中，如果你给属性<code>categories</code>增加一个元素值，模板上显示的数据不会自动更新。为了演示这个特性在组件中增加一个触发属性变化的按钮，首先需要在组件类<code>app/components/store-categories.js</code>中增加一个<code>action</code>方法（有关action会在后面的章节介绍，暂时把他看做是一个普通的js函数），然后在<code>app/templates/components/store-categories.hbs</code>中增加一个触发的按钮。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    <span class="comment">// willRender方法在组件渲染的时候执行，更多有关组件的介绍会在后面章节——组件，中介绍</span></span><br><span class="line">    willRender: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">//  设置一个对象到属性“categories”上，并且设置到categories属性上的对象结构是：key为字符串，value为数组</span></span><br><span class="line">        <span class="built_in">this</span>.set(<span class="string">&#x27;categories&#x27;</span>, &#123;</span><br><span class="line">          <span class="string">&#x27;Bourbons&#x27;</span>: [<span class="string">&#x27;Bulleit&#x27;</span>, <span class="string">&#x27;Four Roses&#x27;</span>, <span class="string">&#x27;Woodford Reserve&#x27;</span>],</span><br><span class="line">          <span class="string">&#x27;Ryes&#x27;</span>: [<span class="string">&#x27;WhistlePig&#x27;</span>, <span class="string">&#x27;High West&#x27;</span>]</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;,</span><br><span class="line">    actions: &#123;</span><br><span class="line">        addCategory: <span class="function"><span class="keyword">function</span>(<span class="params">category</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;清空数据&#x27;</span>);</span><br><span class="line">            <span class="keyword">let</span> categories = <span class="built_in">this</span>.get(<span class="string">&#x27;categories&#x27;</span>);</span><br><span class="line">            <span class="comment">// console.log(categories);</span></span><br><span class="line">            categories[<span class="string">&#x27;Bourbons&#x27;</span>] = [];</span><br><span class="line">            <span class="comment">//  手动执行重渲染方法更新dom元素，但是并没有达到预期效果</span></span><br><span class="line">            <span class="comment">// 还不知道是什么原因</span></span><br><span class="line">            <span class="built_in">this</span>.rerender();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- // templates/components/store-categories.hbs --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">  &#123;&#123;#each-in categories as |category products|&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>&#123;&#123;category&#125;&#125;</span><br><span class="line">      <span class="tag">&lt;<span class="name">ol</span>&gt;</span></span><br><span class="line">        &#123;&#123;#each products as |product|&#125;&#125;</span><br><span class="line">          <span class="tag">&lt;<span class="name">li</span>&gt;</span>&#123;&#123;product&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">        &#123;&#123;/each&#125;&#125;</span><br><span class="line">      <span class="tag">&lt;/<span class="name">ol</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">  &#123;&#123;/each-in&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">button</span> <span class="attr">onclick</span>=<span class="string">&#123;&#123;action</span> &#x27;<span class="attr">addCategory</span>&#x27;&#125;&#125;&gt;</span>点击清空数据<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>但是很遗憾，即使是手动调用了<code>rerender</code>方法也没办法触发重渲染，界面显示的数据并没有发生变化。后续找到原因后再补上！！</p>
<h3 id="空数组处理"><a href="#空数组处理" class="headerlink" title="空数组处理"></a>空数组处理</h3><p>空数组处理与表达式<code>{{each}}</code>一样，同样是判断属性不是<code>null</code>、<code>undefined</code>、<code>[]</code>就显示出数据，否则执行<code>else</code>部分。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123;#each-in people as |name person|&#125;&#125;</span><br><span class="line">  Hello, &#123;&#123;name&#125;&#125;! You are &#123;&#123;person.age&#125;&#125; years old.</span><br><span class="line">&#123;&#123;else&#125;&#125;</span><br><span class="line">  Sorry, nobody is here.</span><br><span class="line">&#123;&#123;/each-in&#125;&#125;</span><br></pre></td></tr></table></figure>

<p>可以参考上一篇的<code>{{each}}</code>标签测试，这里不再赘述。<br><br><br>博文完整代码放在<a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/my_emberjs_code">Github</a>（博文经过多次修改，博文上的代码与github代码可能又出入，不过影响不大！），如果你觉得博文对你有点用，请在github项目上给我点个<code>star</code>吧。您的肯定对我来说是最大的动力！！</p>

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/templetes/handlebars%E6%9D%A1%E4%BB%B6%E8%A1%A8%E8%BE%BE%E5%BC%8F/" rel="next" title="handlebars条件表达式">
          handlebars条件表达式
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/templetes/action%20helper/" rel="prev" title="action helper">
            action helper
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9C%A8%E7%BB%84%E4%BB%B6%E4%B8%AD%E5%A2%9E%E5%8A%A0%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE"><span class="toc-text">在组件中增加测试数据</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%87%8D%E6%B8%B2%E6%9F%93"><span class="toc-text">重渲染</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A9%BA%E6%95%B0%E7%BB%84%E5%A4%84%E7%90%86"><span class="toc-text">空数组处理</span></a>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: 'handlebars显示对象的键', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
